package com.zhongli.devplatform.component;

import com.zhongli.devplatform.constant.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Set;

/**
 * 关注，粉丝
 * @author WuXiCheng
 */
@Component
public class FollowUtil {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 关注或者取消关注
     * @param userId
     * @param followingId
     * @return
     */
    public int addOrRelease(String userId, String followingId) {
        if (userId == null || followingId == null) {
            return -1;
        }
        int isFollow = 0; // 0 = 取消关注 1 = 关注
        String followingKey = Consts.USER_ATT + userId;
        String fansKey = Consts.USER_FANS + followingId;
        if (redisTemplate.opsForZSet().rank(followingKey, followingId) == null) { // 说明userId没有关注过followingId
            redisTemplate.opsForZSet().add(followingKey,followingId, System.currentTimeMillis());
            redisTemplate.opsForZSet().add(fansKey, userId, System.currentTimeMillis());
            isFollow = 1;
        } else { // 取消关注
            redisTemplate.opsForZSet().remove(followingKey,followingId);
            redisTemplate.opsForZSet().remove(fansKey, userId);
        }
        return isFollow;
    }


    /**
     * 验证两个用户之间的关系
     * 0=没关系  1=自己 2=userId关注了otherUserId 3= otherUserId是userId的粉丝 4=互相关注
     * @param userId
     * @param otherUserId
     * @return
     */
    public int checkRelations(String userId, String otherUserId) {

        if (userId == null || otherUserId == null) {
            return 0;
        }

        if (userId.equals(otherUserId)) {
            return 1;
        }
        String followingKey = Consts.USER_ATT + userId;
        int relation = 0;
        if (redisTemplate.opsForZSet().rank(followingKey, otherUserId) != null) { // userId是否关注otherUserId
            relation = 2;
        }
        String fansKey = Consts.USER_FANS + userId;
        if (redisTemplate.opsForZSet().rank(fansKey, userId) != null) {// userId粉丝列表中是否有otherUserId
            relation = 3;
        }
        if ((redisTemplate.opsForZSet().rank(followingKey, otherUserId) != null)
                && redisTemplate.opsForZSet().rank(fansKey, userId) != null) {
            relation = 4;
        }
        return relation;
    }

    /**
     * 获取用户所有关注的人的id
     * @param userId
     * @return
     */
    public Set<String> findFollwings(String userId) {
        return findSet(Consts.USER_ATT + userId);
    }

    /**
     * 获取用户所有的粉丝
     * @param userId
     * @return
     */
    public Set<String> findFans(String userId) {
        return findSet(Consts.USER_FANS + userId);
    }


    /**
     * 根据key获取set
     * @param key
     * @return
     */
    private Set<String> findSet(String key) {
        if (key == null) {
            return new HashSet<>();
        }
        return redisTemplate.opsForZSet().reverseRange(key, 0, -1);
    }

}
